#!/bin/bash

# Kubernetes 部署 Loki + Promtail + Grafana
# 专用于 EC2 Node 模式（使用 DaemonSet）

set -e

echo "开始部署 Loki 日志系统到 Kubernetes (EC2 模式)..."

# 创建命名空间
echo "创建 monitor 命名空间..."
kubectl apply -f loki/deployment.yaml

# 添加 Helm 仓库
echo "添加 Grafana Helm 仓库..."
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update

# 部署 Loki
echo "部署 Loki..."
helm upgrade --install loki grafana/loki \
  --version 6.30.0 \
  -n monitor \
  -f loki/values.yaml

# 部署 Promtail (EC2 Node 模式)
echo "部署 Promtail (DaemonSet 模式)..."
helm upgrade --install promtail grafana/promtail \
  --version 6.15.0 \
  -n monitor \
  -f promtail/values.yaml

# 部署 Grafana
echo "部署 Grafana..."
helm upgrade --install grafana grafana/grafana \
  --version 8.3.0 \
  -n monitor \
  -f grafana/values.yaml

echo ""
echo "=========================================="
echo "EC2 模式部署完成！"
echo "=========================================="
echo ""
echo "获取 Grafana 管理员密码:"
echo "  kubectl get secret -n monitor grafana -o jsonpath='{.data.admin-password}' | base64 -d ; echo"
echo ""
echo "访问 Grafana:"
echo "  kubectl port-forward -n monitor svc/grafana 3000:80"
echo "  然后在浏览器中访问 http://localhost:3000"
echo ""
echo "查看 Promtail DaemonSet 状态:"
echo "  kubectl get ds -n monitor"
echo ""